//
//  LGCreateTeamInputView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/16.
//  创建团队--输入

#import "LGCreateTeamInputView.h"

@interface LGCreateTeamInputView()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , copy)NSString *placeHolder;


@end

@implementation LGCreateTeamInputView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.placeHolder = @"请输入...";
        self.contentTF.text = self.placeHolder;
        [self addSubview:self.titleLabel];
        [self addSubview:self.contentTF];
        [self addSubview:self.numberLabel];
        [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(5));
            make.left.equalTo(self).offset(viewPix(16));
            make.right.equalTo(self).offset(-viewPix(16));
            make.bottom.equalTo(self).offset(-viewPix(40));
        }];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-viewPix(16));
            make.bottom.equalTo(self).offset(-viewPix(5));
            make.height.equalTo(@(viewPix(30)));
        }];
    }
    return self;
}

-(void)setMaxNum:(NSInteger)maxNum{
    _maxNum = maxNum;
    self.numberLabel.text = [NSString stringWithFormat:@"0 / %ld",maxNum];
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.contentTF.text = content;
    if(content.length == 0 || [content isEqualToString:self.placeHolder]){
        self.contentTF.text = self.placeHolder;
        self.contentTF.textColor = [UIColor colorWithString:@"#999999"];
    }else{
        self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:self.placeHolder]){
        self.contentTF.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    NSString *content = textView.text;
    if(content.length == 0||[textView.text isEqualToString:self.placeHolder]){
        self.content = @"";
        self.contentTF.text = self.placeHolder;
        self.contentTF.textColor = [UIColor colorWithString:@"#999999"];
    }else{
        self.content = textView.text;
        self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
    }
//    if(self.delegate && [self.delegate respondsToSelector:@selector(inputContentChanged:content:)]){
//        [self.delegate inputContentChanged:self.questionId content:content];
//    }
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    NSString *content = textView.text;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            textView.text = (textView.text.length>self.maxNum)?[textView.text substringToIndex:self.maxNum]:textView.text;
            self.numberLabel.text = [NSString stringWithFormat:@"%ld / %ld",textView.text.length,self.maxNum];
        }
    }else{
        textView.text = (textView.text.length>self.maxNum)?[textView.text substringToIndex:self.maxNum]:textView.text;
        self.numberLabel.text = [NSString stringWithFormat:@"%ld / %ld",textView.text.length,self.maxNum];
    }
 
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"团队" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(16), viewPix(16), viewPix(100), viewPix(25));
    }
    return _titleLabel;
}



-(UITextView *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextView alloc]init];
        _contentTF.textContainer.lineFragmentPadding = 0;
        _contentTF.textContainerInset = UIEdgeInsetsZero;
        _contentTF.tintColor = [UIColor colorWithString:@"#333333"];
        _contentTF.textColor = [UIColor colorWithString:@"#999999"];
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.font = LGFont(14);
        _contentTF.delegate = self;
    }
    return _contentTF;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"0 / 500" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _numberLabel;
}


@end
